﻿Imports HMSDataLayer
Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports System.Windows.Forms

Public Class PatientRegistrationWindow
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub SalesBillWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        NewBill()
    End Sub

    Private Sub NewBill()
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)
        lst = dataProvider.GetLookupList(LookupTypes.Relationship)
        RelationshipDrComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.PatientType)
        PatientTypeComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.MaritalStatus)
        MaritalStatusComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.Language)
        LanguageComboBox.ItemsSource = lst
        LanguageComboBox.Text = "Marathi"

        lst = dataProvider.GetLookupList(LookupTypes.Religion)
        ReligionComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.BloodGroup)
        BloodGroupComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.Diet)
        DietComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.HealthStatus)
        healthStatusComboBox.ItemsSource = lst

        lst = dataProvider.GetDoctorsLookList()
        AssignedDrComboBox.ItemsSource = lst

        RegDatePicker.SelectedDate = Date.Now
        RegDatePicker.DisplayDateEnd = Now.Date
        DOBDatePicker.DisplayDateEnd = Now.Date
        PatientObject = New PatientRegistration
        GuardianDataGrid.ItemsSource = PatientObject.PatientGuardians
    End Sub

    Private Sub SalesBillWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Sub TransSidePanel_AddButtonClickEvent() Handles TransSidePanel.AddButtonClickEvent
        AddNew()
    End Sub

    Private Sub ApplyPermission()
        If AppCommon.loginInfo.IsSuperUser = True Then
            TransSidePanel.AddVisibility = Visibility.Visible
            TransSidePanel.FindVisibility = Visibility.Visible
            TransSidePanel.SaveVisibility = Visibility.Visible
            TransSidePanel.PrintVisibility = Visibility.Visible
            Exit Sub
        End If
        Dim mp As ModulePermissions
        mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = HMSFormName.PatientRegistration Select ml).FirstOrDefault
        If mp.Add = False Then TransSidePanel.AddVisibility = Visibility.Collapsed Else TransSidePanel.AddVisibility = Visibility.Visible
        If mp.Edit = False Then TransSidePanel.FindVisibility = Visibility.Collapsed Else TransSidePanel.FindVisibility = Visibility.Visible
        If mp.Print = False Then TransSidePanel.PrintVisibility = Visibility.Collapsed Else TransSidePanel.PrintVisibility = Visibility.Visible
    End Sub

    Private Sub TransSidePanel_BackButtonClickEvent() Handles TransSidePanel.BackButtonClickEvent
        ApplyPermission()
        TransSidePanel.BackVisibility = Visibility.Collapsed
        BackView()
    End Sub

    Private Sub TransSidePanel_CancelButtonClickEvent() Handles TransSidePanel.CancelButtonClickEvent
        Me.Close()
    End Sub

    Private Sub TransSidePanel_FindButtonClickEvent() Handles TransSidePanel.FindButtonClickEvent
        TransSidePanel.PrintVisibility = Visibility.Collapsed
        TransSidePanel.AddVisibility = Visibility.Collapsed
        TransSidePanel.SaveVisibility = Visibility.Collapsed
        TransSidePanel.FindVisibility = Visibility.Collapsed
        TransSidePanel.BackVisibility = Visibility.Visible
        FindData()
    End Sub

    Private Sub TransSidePanel_PrintButtonClickEvent() Handles TransSidePanel.PrintButtonClickEvent

    End Sub

    Private Sub TransSidePanel_SaveButtonClickEvent() Handles TransSidePanel.SaveButtonClickEvent
        If SaveData() = True Then

        End If
    End Sub

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If String.IsNullOrEmpty(FirstNameTextBox.Text) Then
                CommonMessages.ShowValidationMsg("Please enter first name!")
                Exit Function
            End If

            'If Trim(LastNameTextBox.Text) = "" Then
            '    CommonMessages.ShowValidationMsg("Please enter last name!")
            '    Exit Function
            'End If

            If String.IsNullOrEmpty(ContactNoTextBox.Text) And String.IsNullOrEmpty(MobileNoTextBox.Text) Then
                CommonMessages.ShowValidationMsg("Please enter telephone/mobile no!")
                Exit Function
            End If

            If String.IsNullOrEmpty(AgeTextBox.Text) Then
                CommonMessages.ShowValidationMsg("Please enter age!")
                Exit Function
            End If

            'If Trim(PatientTypeComboBox.Text) = "" Then
            '    CommonMessages.ShowValidationMsg("Please select patient type!")
            '    Exit Function
            'End If

            'If Trim(DOBDatePicker.Text) = "" Then
            '    CommonMessages.ShowValidationMsg("Please select DOB date!")
            '    Exit Function
            'End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  [PatientId] FROM dbo.PatientRegistration WHERE  ([FirstName] = N'" & FirstNameTextBox.Text.Trim & "') AND ([LastName] = N'" & LastNameTextBox.Text.Trim & "')"
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Patient already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
        Dim dataProvider As New PatientDataLayer()
        PatientObject = dataProvider.GetPatientByID(ID)
        If PatientObject Is Nothing Then Exit Sub
        FirstNameTextBox.Text = PatientObject.FirstName
        LastNameTextBox.Text = PatientObject.LastName
        SurNameTextBox.Text = PatientObject.SurName
        If PatientObject.Gender = Genders.Male Then
            MaleRadioButton.IsChecked = True
        Else
            FemaleRadioButton.IsChecked = True
        End If
        AddressTextBox.Text = PatientObject.Address
        TelNoTextBox.Text = PatientObject.TelephoneNo
        MobileNoTextBox.Text = PatientObject.MobileNo
        EmailTextBox.Text = PatientObject.Email
        RemarksTextBox.Text = PatientObject.Remarks
        PatientTypeComboBox.SelectedValue = PatientObject.PatientType
        RegDatePicker.SelectedDate = PatientObject.RegistrationDate
        ReferedDrTextBox.Text = PatientObject.RefByDoc
        DOBDatePicker.SelectedDate = PatientObject.DOB
        AgeTextBox.Text = PatientObject.Age
        OccupationTextBox.Text = PatientObject.Occupation
        AssignedDrComboBox.SelectedValue = PatientObject.AssignedDr
        MaritalStatusComboBox.SelectedValue = PatientObject.MaritalStatus
        LanguageComboBox.SelectedValue = PatientObject.language
        ReligionComboBox.SelectedValue = PatientObject.religion
        BloodGroupComboBox.SelectedValue = PatientObject.bloodGroup
        DietComboBox.SelectedValue = PatientObject.diet
        If PatientObject.siblingBrother IsNot Nothing Then BrotherTextBox.Text = PatientObject.siblingBrother
        If PatientObject.siblingSister IsNot Nothing Then SisterTextBox.Text = PatientObject.siblingSister
        If PatientObject.childBoys IsNot Nothing Then BoysTextBox.Text = PatientObject.childBoys
        If PatientObject.childGirl IsNot Nothing Then GirlsTextBox.Text = PatientObject.childGirl
    End Sub
    Dim PatientObject As PatientRegistration

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New PatientDataLayer()
        PatientObject.FirstName = FirstNameTextBox.Text.Trim
        PatientObject.LastName = LastNameTextBox.Text.Trim
        PatientObject.SurName = SurNameTextBox.Text.Trim
        If MaleRadioButton.IsChecked = True Then
            PatientObject.Gender = Genders.Male
        Else
            PatientObject.Gender = Genders.Female
        End If
        PatientObject.Address = AddressTextBox.Text.Trim
        PatientObject.TelephoneNo = TelNoTextBox.Text.Trim
        PatientObject.MobileNo = MobileNoTextBox.Text.Trim
        PatientObject.Email = EmailTextBox.Text.Trim
        PatientObject.Remarks = RemarksTextBox.Text.Trim
        PatientObject.PatientType = PatientTypeComboBox.SelectedValue
        PatientObject.RegistrationDate = RegDatePicker.SelectedDate
        PatientObject.RefByDoc = ReferedDrTextBox.Text.Trim
        PatientObject.DOB = DOBDatePicker.SelectedDate
        PatientObject.Age = AgeTextBox.Text.Trim
        PatientObject.Occupation = OccupationTextBox.Text.Trim
        PatientObject.AssignedDr = AssignedDrComboBox.SelectedValue
        PatientObject.MaritalStatus = MaritalStatusComboBox.SelectedValue
        PatientObject.language = LanguageComboBox.SelectedValue
        PatientObject.religion = ReligionComboBox.SelectedValue
        PatientObject.bloodGroup = BloodGroupComboBox.SelectedValue
        PatientObject.diet = DietComboBox.SelectedValue
        PatientObject.siblingBrother = Val(BrotherTextBox.Text)
        PatientObject.siblingSister = Val(SisterTextBox.Text)
        PatientObject.childBoys = Val(BoysTextBox.Text)
        PatientObject.childGirl = Val(GirlsTextBox.Text)
        PatientObject.CreatedBy = AppCommon.loginInfo.UserID
        dataProvider.SavePatient(PatientObject, ActionMode)
        If PatientObject.PatientId > 0 Then
            PatientIDTextBlock.Text = PatientObject.PatientId
            ActionMode = FormAction.None
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub AddGuardianButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddGuardianButton.Click
        Dim pGuard As New PatientGuardian
        If String.IsNullOrEmpty(GuardianNameTextBox.Text) Then CommonMessages.ShowValidationMsg("Please enter guardian name") : Exit Sub
        pGuard.GuardianName = GuardianNameTextBox.Text
        pGuard.Relationship = RelationshipDrComboBox.Text
        pGuard.RelationshipID = RelationshipDrComboBox.SelectedValue
        pGuard.ContactNo = ContactNoTextBox.Text
        pGuard.age = GuardianAgeTextBox.Text
        pGuard.healthStatus = healthStatusComboBox.SelectedValue
        pGuard.healthStatusName = healthStatusComboBox.Text
        pGuard.Complaints = GuardianComplaintsTextBox.Text
        pGuard.notes = GuardianNotesTextBox.Text

        PatientObject.PatientGuardians.Add(pGuard)
        RelationshipDrComboBox.SelectedIndex = -1
        GuardianNameTextBox.Text = String.Empty
        ContactNoTextBox.Text = String.Empty
    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        NewBill()
        FirstNameTextBox.Text = String.Empty
        LastNameTextBox.Text = String.Empty
        SurNameTextBox.Text = String.Empty
        MaleRadioButton.IsChecked = True
        AddressTextBox.Text = String.Empty
        TelNoTextBox.Text = String.Empty
        MobileNoTextBox.Text = String.Empty
        EmailTextBox.Text = String.Empty
        RemarksTextBox.Text = String.Empty
        PatientTypeComboBox.SelectedIndex = -1
        ReferedDrTextBox.Text = String.Empty
        DOBDatePicker.SelectedDate = Nothing
        AgeTextBox.Text = String.Empty
        OccupationTextBox.Text = String.Empty
        AssignedDrComboBox.SelectedIndex = -1
        MaritalStatusComboBox.SelectedIndex = -1
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainStack.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As PatientDataLayer
        Dim DataCollection As Object
        DataObject = New PatientDataLayer()
        DataCollection = DataObject.GetPatientList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainStack.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub DOBDatePicker_CalendarClosed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles DOBDatePicker.CalendarClosed
        Dim ts As TimeSpan
        ts = Date.Now.Subtract(DOBDatePicker.SelectedDate)
        Dim years As Integer = ts.Days / 365
        Dim days As Integer = ts.Days Mod 365
        AgeTextBox.Text = years.ToString & "." & days.ToString
    End Sub

    Private Sub SearchDataGrid_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles SearchDataGrid.MouseDoubleClick
        Dim patient As PatientRegistration = sender.SelectedItem
        If patient IsNot Nothing Then
            LoadData(patient.PatientId)
            TransSidePanel_BackButtonClickEvent()
        End If
    End Sub

    Private Sub ImageButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ImageButton.Click
        Dim dlg As New OpenFileDialog
        dlg.ShowDialog()

        If String.IsNullOrEmpty(dlg.FileName) = False Then
            Dim bmImage As New BitmapImage()
            bmImage.BeginInit()
            bmImage.UriSource = New Uri(dlg.FileName, UriKind.Absolute)
            bmImage.EndInit()
            PatientImage.Source = bmImage
            PatientObject.image = IO.File.ReadAllBytes(dlg.FileName)
        End If
    End Sub
End Class
